import z from 'zod'
import { request } from '.'

// 获得教师信息
const teacherInfoValidator = z.object({
  entry: z.object({
    id: z.number(),
    timestamp: z.string(),
    birthday: z.string(),
    gender: z.string(),
    name: z.string(),
    phone: z.string(),
    school: z.string(),
    stageType: z.number(),
    teacherId: z.string(),
    dueDate: z.string(),
    title: z.union([z.string(), z.null()]),
    email: z.union([z.string(), z.null()]),
  }),
})
export type TeacherInfo = z.infer<typeof teacherInfoValidator>
export function getTeacherInfo() {
  return request({
    method: 'POST',
    url: '/get-teacher',
    responseValidator: teacherInfoValidator,
  })
}

// 修改教师信息
const UpdateTeacherInfoRequestSchema = z.object({
  birthday: z.string(),
  gender: z.string(),
  name: z.string(),
  data: z.string(),
  school: z.string(),
  stageType: z.number(),
})
export function updateTeacherInfo(data: z.infer<typeof UpdateTeacherInfoRequestSchema>) {
  return request({
    method: 'POST',
    url: '/set-teacher',
    data,
    dataValidator: UpdateTeacherInfoRequestSchema,
    responseValidator: z.object({
      entry: z.object({
        birthday: z.string(),
        dueDate: z.string(),
        gender: z.string(),
        name: z.string(),
        partitionKey: z.string(),
        phone: z.string(),
        rowKey: z.string(),
        school: z.string(),
        stageType: z.number(),
        teacherId: z.string(),
        timestamp: z.string(),
      }),
    }),
  })
}

// 获得教师所在学校集合
export function getTeacherSchools() {
  return request({
    method: 'POST',
    url: '/get-teacher-school',
    responseValidator: z.object({
      entries: z.array(z.object({
        active: z.union([z.number(), z.null()]).optional(),
        alias: z.union([z.null(), z.string()]).optional(),
        city: z.union([z.null(), z.string()]).optional(),
        citySchool: z.union([z.null(), z.string()]).optional(),
        createTime: z.union([z.null(), z.string()]).optional(),
        dueDate: z.union([z.null(), z.string()]).optional(),
        joinTime: z.union([z.null(), z.string()]).optional(),
        maxLimit: z.union([z.number(), z.null()]).optional(),
        phone: z.union([z.null(), z.string()]).optional(),
        province: z.union([z.null(), z.string()]).optional(),
        schoolId: z.union([z.null(), z.string()]).optional(),
        schoolName: z.union([z.null(), z.string()]).optional(),
        stage: z.union([z.null(), z.string()]).optional(),
        stageType: z.union([z.number(), z.null()]).optional(),
        studentCount: z.union([z.number(), z.null()]).optional(),
        teacherCount: z.union([z.number(), z.null()]).optional(),
        teacherId: z.union([z.null(), z.string()]).optional(),
        teacherName: z.union([z.null(), z.string()]).optional(),
      })),
    }),
  })
}

// 通过学校邀请码查询学校并建立教师学校关系
const relateTeacherSchoolRequestSchema = z.object({
  invitationCode: z.string(),
})
export function relateTeacherSchool(data: z.infer<typeof relateTeacherSchoolRequestSchema>) {
  return request({
    method: 'POST',
    url: '/set-teacher-by-code',
    data,
    dataValidator: relateTeacherSchoolRequestSchema,
    responseValidator: z.object({
      entry: z.object({
        birthday: z.string(),
        dueDate: z.string(),
        gender: z.string(),
        name: z.string(),
        partitionKey: z.string(),
        phone: z.string(),
        rowKey: z.string(),
        school: z.string(),
        stageType: z.number(),
        teacherId: z.string(),
        timestamp: z.string(),
      }),
    }),
  })
}
